package view
{
	import flash.events.Event;
	
	import model.AnneeEtude;
	import model.AnneeUniversitaire;
	import model.Enseignant;
	import model.Enseignement;
	import model.Formation;
	import model.Matiere;
	import model.Service;
	import model.proxy.ServiceProxy;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import view.components.ServicePanel; 
	
	public class ServicePanelMediator extends Mediator implements IMediator 
	{ 
		
		public static const NAME : String = 'ServicePanelMediator'; 
			
		/** 
		 * Constructor 
		 * Listen event of the component to send notification 
		 */
		 public function ServicePanelMediator(viewComponent : ServicePanel) 
		 {
		 	super(NAME, viewComponent); 
		 	viewComponent.addEventListener(ServicePanel.OBTENIR_LISTE_ANNEES_UNIVERSITAIRE_SERVICE, obtenirListeAnneesUniversitaireNotification);
		 	viewComponent.addEventListener(ServicePanel.OBTENIR_LISTE_ANNEES_ETUDE_SERVICE, obtenirListeAnneesEtudeNotification);
		 	viewComponent.addEventListener(ServicePanel.OBTENIR_LISTE_ENSEIGNEMENTS_SERVICE, obtenirListeEnseignementsNotification);
		 	viewComponent.addEventListener(ServicePanel.OBTENIR_LISTE_ENSEIGNANTS_SERVICE, obtenirListeEnseignantsNotification);
		 	viewComponent.addEventListener(ServicePanel.OBTENIR_LISTE_SERVICES, obtenirListeServicesNotification);
		 	viewComponent.addEventListener(ServicePanel.VALIDER_AJOUT_SERVICE, validerAjoutServiceNotification);
		 	viewComponent.addEventListener(ServicePanel.VALIDER_MODIF_SERVICE, validerModifServiceNotification);
		 	viewComponent.addEventListener(ServicePanel.SUPPRIMER_SERVICE, supprimerServiceNotification);
		 }
		 
		 
		 private function obtenirListeAnneesUniversitaireNotification(event : Event=null):void
		 {  
			 sendNotification(ServicePanel.OBTENIR_LISTE_ANNEES_UNIVERSITAIRE_SERVICE, null); 
		 } 
		 
		 private function obtenirListeAnneesEtudeNotification(event : Event=null):void
		 {  
		 	 var anneeUniversitaire:AnneeUniversitaire = (servicePanel.comboAnneeUniversitaire.selectedItem as AnneeUniversitaire); 
			 sendNotification(ServicePanel.OBTENIR_LISTE_ANNEES_ETUDE_SERVICE, anneeUniversitaire); 
		 } 

		private function obtenirListeEnseignementsNotification(event : Event=null):void
		 {  
			 var matiere:Matiere = (servicePanel.comboMatiere.selectedItem as Matiere);
			 var anneeEtude:AnneeEtude = (servicePanel.comboAnneeEtude.selectedItem as AnneeEtude); 
			 sendNotification(ServicePanel.OBTENIR_LISTE_ENSEIGNEMENTS_SERVICE, [anneeEtude, matiere]); 
		 } 
		 
		 private function obtenirListeEnseignantsNotification(event : Event=null):void
		 {  
			 var anneeUniversitaire:AnneeUniversitaire = (servicePanel.comboAnneeUniversitaire.selectedItem as AnneeUniversitaire);
			 var formation:Formation = ((servicePanel.comboAnneeEtude.selectedItem as AnneeEtude).collComposerFs as Array)[0].aFormation as Formation; 
			 sendNotification(ServicePanel.OBTENIR_LISTE_ENSEIGNANTS_SERVICE, [anneeUniversitaire, formation]); 
		 } 
		 
		 private function obtenirListeServicesNotification(event : Event=null):void
		 {  
			 var anneeEtude:AnneeEtude = (servicePanel.comboAnneeEtude.selectedItem as AnneeEtude); 
			 sendNotification(ServicePanel.OBTENIR_LISTE_SERVICES, anneeEtude); 
		 }
		 
		 private function validerAjoutServiceNotification(event : Event=null):void
		 {
		 	if ((servicePanel.comboAnneeEtude.selectedItem == null)||(servicePanel.comboEnseignant.selectedItem == null)||(servicePanel.comboEnseignement.selectedItem == null))
		 		Alert.show("Veuillez a bien choisir une année d'étude, un enseignant et un enseignement");
		 	else
			 	{
			 	var service:Service = new Service();
			 	var anneeEtude:AnneeEtude = servicePanel.comboAnneeEtude.selectedItem as AnneeEtude;
			 	service.duree_service = parseInt(servicePanel.nb_h_enseignants.text);
			 	service.id_an_etude = (servicePanel.comboAnneeEtude.selectedItem as AnneeEtude).id_an_etude;
			 	service.id_enseignant = (servicePanel.comboEnseignant.selectedItem as Enseignant).id_enseignant;
			 	service.id_enseignement = (servicePanel.comboEnseignement.selectedItem as Enseignement).id_enseignement;
			 	sendNotification(ServicePanel.VALIDER_AJOUT_SERVICE, [service, anneeEtude]);
			 	}
		 }
		 
		 private function validerModifServiceNotification(event : Event=null):void
		 {
		 	var service:Service = servicePanel.dgService.selectedItem.valueOf() as Service;
		 	var anneeEtude:AnneeEtude = servicePanel.comboAnneeEtude.selectedItem as AnneeEtude;
		 	service.duree_service = parseInt(servicePanel.nb_h_enseignants.text);
		 	sendNotification(ServicePanel.VALIDER_MODIF_SERVICE, [service, anneeEtude]);
		 }
		 
		 private function supprimerServiceNotification(event : Event=null):void
		 {
		 	var service:Service = servicePanel.dgService.selectedItem.valueOf() as Service;
		 	var anneeEtude:AnneeEtude = servicePanel.comboAnneeEtude.selectedItem as AnneeEtude;
		 	sendNotification(ServicePanel.SUPPRIMER_SERVICE, [service, anneeEtude]);
		 }
		 
		 override public function listNotificationInterests():Array 
		 { 
		 	return [ 
		 			ServiceProxy.LISTE_ANNEES_ETUDE_SERVICE_OBTENUE,
		 			ServiceProxy.LISTE_ANNEES_UNIVERSITAIRE_SERVICE_OBTENUE,
		 			ServiceProxy.LISTE_ENSEIGNEMENTS_SERVICE_OBTENUE,
		 			ServiceProxy.LISTE_ENSEIGNANTS_SERVICE_OBTENUE,
		 			ServiceProxy.LISTE_SERVICES_OBTENUE
		 			]; 
		 } 
		 

		 override public function handleNotification(note : INotification):void 
		 { 
			 switch (note.getName()) 
			 { 
			 	case ServiceProxy.LISTE_ANNEES_UNIVERSITAIRE_SERVICE_OBTENUE:
			 	{
			 		servicePanel.comboAnneeUniversitaire.dataProvider = (note.getBody() as Array);
			 		if ((servicePanel.comboAnneeUniversitaire.dataProvider.source as Array).length > 0)
			 			servicePanel.choixComboAnneeUniversitaire();
			 		break;
			 	}
			 	case ServiceProxy.LISTE_ANNEES_ETUDE_SERVICE_OBTENUE:
			 	{
			 		servicePanel.comboAnneeEtude.dataProvider = (note.getBody() as Array);
			 		if ((servicePanel.comboAnneeEtude.dataProvider.source as Array).length > 0)
			 			servicePanel.choixComboAnneeEtude();	
			 		break;
			 	}
			 	case ServiceProxy.LISTE_ENSEIGNEMENTS_SERVICE_OBTENUE:
			 	{
			 		servicePanel.comboEnseignement.dataProvider = (note.getBody() as Array);
			 		if((servicePanel.comboEnseignement.dataProvider.source as Array).length ==0)
					{
						servicePanel.comboEnseignement.enabled = false;
					}
			 		break;
			 	}
			 	case ServiceProxy.LISTE_ENSEIGNANTS_SERVICE_OBTENUE:
			 	{
			 		servicePanel.comboEnseignant.dataProvider = (note.getBody() as Array);
					if((servicePanel.comboEnseignant.dataProvider.source as Array).length ==0)
					{
						servicePanel.comboEnseignant.enabled = false;
					}
			 		break;
			 	}
			 	case ServiceProxy.LISTE_SERVICES_OBTENUE:
			 	{
			 		servicePanel.dgService.dataProvider = (note.getBody() as Array);
			 		break;
			 	}
			 	default:
			 	break;
		 	} 
		 } 
		 
		 /** 
		 * Get the loginPanel component 
		 */ 
		 private function get servicePanel():ServicePanel 
		 { 
		 	return viewComponent as ServicePanel; 
		 }
	}
}